const app = getApp()
const WXAPI = require("../../../api/newApi")
import * as echarts from '../../ec-canvas/echarts';
let chart;
function setOption(canvas, width, height,dpr,bdata) {
  console.log(bdata)
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    grid:{
      x:32,
      x2:22,
    },
    xAxis: {
      splitNumber: 6,
      type: 'category',
      axisLine:{
        lineStyle:{
            color:'#E8E9EC',
            width:1,//这里是为了突出显示加上的
        }
      },
      boundaryGap: false,
      axisTick: { //x轴刻度
        show: false
      },
      // data: ['0.00', '06.00', '12.00', '18.00', '24.00'],
      data:['0.00','1.00','2.00','3.00','4.00','5.00','6.00','7.00','8.00','9.00','10.00','11.00','12.00','13.00','14.00','15.00','16.00','17.00','18.00','19.00','20.00','21.00','22.00','23.00','24.00'],
      splitLine: {//坐标轴在 grid 区域中的分隔线。
        show: true,
        lineStyle: {
          type: 'dashed',
          color:'#EBEDEE',
        }
      },
      axisLabel: {
        showMaxLabel:true,
        textStyle: {
          fontSize: 13,
          color: '#999999'
        }

      }
  },
  yAxis: {
      type: 'value',
      axisLine:{
        lineStyle:{
            color:'#fff',
            width:1,//这里是为了突出显示加上的
        }
      },
      splitLine: {//坐标轴在 grid 区域中的分隔线。
        show: true,
        lineStyle: {
          type: 'dashed',
          color:'#EBEDEE',
        }
      },
      axisLabel: {
        textStyle: {
          fontSize: 13,
          color: '#999999'
        }
      }
  },
  itemStyle :{
    color: {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [{
            offset: 0.5, color: '#FF9D69' // 0% 处的颜色
        },{
            offset: 1, color: 'rgba(255, 157, 105, 0)' // 100% 处的颜色
        }],
        global: false // 缺省为 false
    }
},
  series: [
    {
      data: [0, 0, 0, 0, 0],
      showSymbol: true, //去掉折现小圆点
      showSymbol: true, //hover才有的样式
      smooth: true, // 平滑曲线
      // data: bdata,
      type: 'line',
      areaStyle: {},
      label: {
        show: true,
        position: 'top',
        // formatter: (param) => {
        //   console.log(param.data)
        //   console.log(param)
        //   console.log(param.dataIndex)
        //   // if (this.data.daka_type === 'exercise_mission') {
        //   //   return utils.toThousand(param.value)
        //   // } else {
        //   //   return param.value + '次'
        //   // }
        //   if(param.dataIndex==4){
        //     return param.data
        //   }else{
        //     return ''
        //   }
          
        // },
        color: '#FF5800',
        fontSize: 12
      },
    }
  ]
  };
  chart.setOption(option);
  return chart;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp: app.globalData.imgHttp, //图片前缀
    tab:[
      {id:0,value:'股东分红'},{id:1,value:'拉新分红'}
    ],
    gdTab:[
      {id:0,value:'今日'},{id:1,value:'本周'},{id:2,value:'本月'}
    ],
    changeIndex: 0,
    tabIndex: 0,
    ec: {
      onInit: setOption,
      // lazyLoad: true, // 延迟加载
    },
    laxLs:[],
    shareImg:'',
    assetId: '',
    id:'',
    info:{},
    dd:[],
    type: 1,//查询类型  1 今日，2 本周 3 本月
    dataDay: [], //今日数据
    dataWeek:[], //本周数据
    dataMonth:[], //本月数据
    dataOptions: [],
    pageSize: 3,
    pageNum: 1,
    total: 0, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight:app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    this.data.assetId = options.assetId
    this.data.id = options.id
    this.getDetail()
    this.getCommission()
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(this.data.info.referrerId)
    return {
      title: '成为' + this.data.info.shopName + '股东，点击加入...',
      path: `/userPageB/pages/userStore/cardDetail/cardDetail?assetId=` + this.data.assetId + '&referrerId=' + this.data.info.referrerId+'&typePath=share',
    }
  },
  getDetail(){
    wx.showLoading({
      title: '加载中',
    })
    // var id = this.data.assetId
    // var url = `/app/v2/customer/asset/card/shareholder/detail/${id}`
    var data = {
      id: this.data.id,
      assetId: this.data.assetId
    }
    WXAPI.cardDetail(data,(res)=>{
      this.setData({
        info: res.result
      })
      setTimeout(()=>{
        this.getDividend()
      },600)
      wx.hideLoading()
    })
  },
  getDividend(){
    var data = {
      id: this.data.info.id,
      merchantId: this.data.info.merchantId,
      assetId: this.data.info.assetId,
      type: this.data.type
    }
    WXAPI.dividend(data,(res)=>{
      let ls = res.result.map(item => {return item.dayDividendMoney})
      console.log(ls)
      let dataX = []
      if(this.data.type==1){
        dataX = ['0.00','1.00','2.00','3.00','4.00','5.00','6.00','7.00','8.00','9.00','10.00','11.00','12.00','13.00','14.00','15.00','16.00','17.00','18.00','19.00','20.00','21.00','22.00','23.00','24.00']
      }else if(this.data.type==2){
        dataX = ['周一','周二','周三','周四','周五','周六','周日']
      }else{
        let ss = res.result.map(item => {return item.time})
        let monthLs = ss.map((item,index)=>{
          if(item.charAt(0)==0){
            item = item.replace('0','')
            console.log(item)
          }else{
            item = item
          }
          return item
        })
        console.log(monthLs)
        dataX = monthLs
      }
      let option3 = {
        xAxis: {
          data: dataX,
        },  
        series: [{  
          data: ls,
          // data: bdata,
          type: 'line',
          areaStyle: {},
          label: {
            show: true,
            position: 'top',
            formatter: (param) => {
              if(param.dataIndex + 1==ls.length){
                return param.data
              }else{
                return ''
              }
              
            },
            color: '#FF5800',
            fontSize: 12
          },
        }]
      }
      chart.setOption(option3)
    })
  },
  getCommission(){ //查询拉新分红 commission
    var data = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
      assetId: this.data.assetId,
    }
    WXAPI.commission(data,(res)=>{
      if (res.code == 200) {
       
        if(res.result.total==-1){
          this.data.total = 0
        }else{
          this.data.total = res.result.total
        }
        this.setData({
          total: this.data.total
        })
        if (this.data.pageNum > 1) {
          this.data.laxLs = this.data.laxLs.concat(res.result.records); //将数据拼接在一起
          this.setData({
            laxLs: this.data.laxLs
          })
        } else {
          this.data.laxLs = res.result.records
          this.setData({
            laxLs: this.data.laxLs
          })
        }
      }
    })
  },
  getmore(){ //点击拉新分红更多
    console.log("触底了",this.data.pageNum,Math.ceil(this.data.total / this.data.pageSize),this.data.total)
    if (this.data.pageNum >= Math.ceil(this.data.total / this.data.pageSize)) {
      console.log('没有分页')
      wx.showToast({
        title: '没有更多了哦~',
        icon:'none'
      })
    } else {
      this.data.pageNum++; //每触底一次 page + 1;
      this.getCommission();
    }
  },
  back(){
    wx.navigateBack({
      delta: 0,
    })
  },
  changeRd(e){ //股东分红、拉新分红选择
    let index = e.currentTarget.dataset.index;
    this.setData({
      changeIndex: index
    })
    if(index==0){
      this.getDividend()
    }
  },
  changeDay(e){ //今日、本周、本月选择
    let index = e.currentTarget.dataset.index;
    this.data.type = index + 1
    console.log(this.data.type)
    this.setData({
      tabIndex: index
    })
    this.getDividend()
  },
  jumpDetail(){ //股东卡详情
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/card_detail/card_detail?assetId=' + this.data.assetId,
    })
  },
  jumpBalance(e){ //明细
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/balanceDetails/balanceDetails?id=' + id,
    })
  },
  jumpHonor(e){ //荣誉分享
    let id  = e.currentTarget.dataset.id
    let assetId = e.currentTarget.dataset.assetid
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/honorShare/honorShare?id=' + id + '&assetId=' + assetId
    })
  },
  junpShop(e){ //跳转店铺
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId='+id,
    })
  },
  //充值
  handletopup(e){
    var id = e.currentTarget.dataset.id
    wx.redirectTo({
      url: '/userPageA/pages/topup/topup?customerCardId='+id,
    })
  },
  //立即使用
  handleConsumption(e){
    var aid = e.currentTarget.dataset.aid;
    var mid = e.currentTarget.dataset.mid;
    wx.redirectTo({
      url: '/pages/pay/pay?assetId='+aid+'&merchantId='+mid,
    })
  },
})